{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 第 10 节　列联表检验\n",
    "## 第 3 章　使用 Python 进行数据分析｜用 Python 动手学统计学"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 5. 实现：计算 p 值"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "# 用于数值计算的库\n",
    "import pandas as pd\n",
    "import scipy as sp\n",
    "# 用于绘图的库\n",
    "import seaborn as sns\n",
    "from scipy import stats\n",
    "\n",
    "sns.set()\n",
    "\n",
    "# 设置浮点数打印精度\n",
    "%precision 3\n",
    "# 在 Jupyter Notebook 里显示图形\n",
    "%matplotlib inline"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:22:31.212916Z",
     "end_time": "2024-04-16T20:22:31.238413Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "0.010"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算 p 值\n",
    "1 - sp.stats.chi2.cdf(x=6.667, df=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:22:31.238413Z",
     "end_time": "2024-04-16T20:22:31.255853Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 6. 实现：列联表检验"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "  color  click  freq\n0  blue  click    20\n1  blue    not   230\n2   red  click    10\n3   red    not    40",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>color</th>\n      <th>click</th>\n      <th>freq</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>blue</td>\n      <td>click</td>\n      <td>20</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>blue</td>\n      <td>not</td>\n      <td>230</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>red</td>\n      <td>click</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>red</td>\n      <td>not</td>\n      <td>40</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读入数据\n",
    "click_data = pd.read_csv(\"3-10-1-click_data.csv\")\n",
    "click_data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:22:31.257794Z",
     "end_time": "2024-04-16T20:22:31.336697Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "click  click  not\ncolor            \nblue      20  230\nred       10   40",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>click</th>\n      <th>click</th>\n      <th>not</th>\n    </tr>\n    <tr>\n      <th>color</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>blue</th>\n      <td>20</td>\n      <td>230</td>\n    </tr>\n    <tr>\n      <th>red</th>\n      <td>10</td>\n      <td>40</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转换为列联表\n",
    "cross = pd.pivot_table(\n",
    "    data=click_data,\n",
    "    values=\"freq\",\n",
    "    aggfunc=\"sum\",\n",
    "    index=\"color\",\n",
    "    columns=\"click\"\n",
    ")\n",
    "cross"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:22:31.283945Z",
     "end_time": "2024-04-16T20:22:31.399223Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "Chi2ContingencyResult(statistic=6.666666666666666, pvalue=0.009823274507519247, dof=1, expected_freq=array([[ 25., 225.],\n       [  5.,  45.]]))"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 进行检验\n",
    "sp.stats.chi2_contingency(cross, correction=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:22:31.317748Z",
     "end_time": "2024-04-16T20:22:31.399724Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T20:22:31.342766Z",
     "end_time": "2024-04-16T20:22:31.415595Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
